import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class Crypto {
	//static String DbPath="/Users/fahadalfaisal/server.db";
	public boolean Decryption() {	
	try{
			String DbPath="/Users/fahadalfaisal/server.db";
            FileInputStream verfile = new FileInputStream("aes.bin");
	        byte[] encKey = new byte[verfile.available()];  
	        verfile.read(encKey);
	        verfile.close();
	        //System.out.println(new String(encKey));	
	        PKCS8EncodedKeySpec pkcs8 = new PKCS8EncodedKeySpec( encKey);
			byte[] decrypted = pkcs8.getEncoded();
			PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec( decrypted );
	        SecretKeySpec skey = new SecretKeySpec(decrypted,"AES");
	        FileInputStream verfile2 = new FileInputStream("iv.bin");
	        byte[] iv = new byte[verfile2.available()];  
	        verfile2.read(iv);
	        Cipher aesDec = Cipher.getInstance("AES/CBC/PKCS5Padding");
	        aesDec.init( Cipher.DECRYPT_MODE,skey,new IvParameterSpec(iv) );
			FileInputStream Db = new FileInputStream(DbPath);
	        byte[] dbfile = new byte[Db.available()];  
	        Db.read(dbfile);
	        byte[] Dbcontent = aesDec.doFinal(dbfile);
	        //System.out.println(new String(Dbcontent));
	        FileOutputStream Dbwr = new FileOutputStream(DbPath);
            Dbwr.write(Dbcontent);
            Dbwr.close();
	        verfile2.close();
	        Db.close();
		}
		catch (Exception e){
			System.out.println("Decryption Database failed");
			return false;
		}
	return true;
	}
	public boolean encryption(){
		SecretKeySpec k;
		SecretKey k1;
		try{
			String DbPath="/Users/fahadalfaisal/server.db";
			File rsa_ver = new File("aes.bin");
			if(!rsa_ver.exists()){
				KeyGenerator gen = KeyGenerator.getInstance("AES");
		        gen.init(256);
		        k1 = gen.generateKey();
		        Cipher ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
				ecipher.init(Cipher.ENCRYPT_MODE, k1);
				byte[] iv = ecipher.getIV();
				FileInputStream Db = new FileInputStream(DbPath);
		        byte[] encKey = new byte[Db.available()];  
		        Db.read(encKey);
		        Db.close();
                byte[] c = ecipher.doFinal(encKey);
                System.out.println(new String (c));              
                FileOutputStream Dbwr = new FileOutputStream(DbPath);
                Dbwr.write(c);
                Dbwr.close();
		        byte[] encPriv = k1.getEncoded();
		        FileOutputStream privfos = new FileOutputStream("aes.bin");
             	privfos.write(encPriv);
             	privfos.close();
             	FileOutputStream riv = new FileOutputStream("iv.bin");
             	riv.write(iv);
             	riv.close();
			}
			else {
				FileInputStream verfile = new FileInputStream("aes.bin");
		        byte[] encKey = new byte[verfile.available()];  
		        verfile.read(encKey);
		        verfile.close();
		        //System.out.println(new String(encKey));	
		        PKCS8EncodedKeySpec pkcs8 = new PKCS8EncodedKeySpec( encKey);
				byte[] decrypted = pkcs8.getEncoded();
				//PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec( decrypted );
		         k = new SecretKeySpec(decrypted,"AES");
		         Cipher ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
					ecipher.init(Cipher.ENCRYPT_MODE, k);
					byte[] iv = ecipher.getIV();
					FileInputStream Db = new FileInputStream(DbPath);
			        byte[] dbcontent = new byte[Db.available()];  
			        Db.read(dbcontent);
	                Db.close();
	                byte[] c = ecipher.doFinal(dbcontent);
	                FileOutputStream Dbwr = new FileOutputStream(DbPath);
	                Dbwr.write(c);
	                Dbwr.close();
	             	FileOutputStream riv = new FileOutputStream("iv.bin");
	             	riv.write(iv);
	             	riv.close();
	     
			}
	               
		}
		catch(Exception e){
			e.printStackTrace();
			System.out.println("AES test failed");	
		    return false;	
		}

		return true;
	}
}
